//获取应用实例
var WxParse = require('../../wxParse/wxParse.js');
let time = require('../util/date.js');
const app = getApp();
Page({
  data: {
    goodsList: [],
    commission_vip: 0,
    commission: 0
  },


  getDeviceInfo: function() {
    let that = this
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
      }
    })
  },

  onReady() {
    this.getDeviceInfo();
  },
  onLoad: function(options) {
    var that = this;
    that.setData({
      goods_id: options.id
    })
    console.log(app.globalData.token)
    if (app.globalData.token == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      setTimeout(function() {
        //要延时执行的代码
        wx.navigateTo({
          url: '/pages/mine/login',
        })
      }, 2000) //延迟时间 这里是1秒
    } else {
      that.getDetail(options.id);
      that.linqu();
    }
  },
  linqu: function (e) {

    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=Pdd&a=judgeUser',
      data: {
        token: app.globalData.token,

      },
      header: {
        'Content-type': 'application/x-www-form-urlencoded',
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {

        console.log('ppp');
        wx.hideLoading();

        if (res.data.code == 0) {
         
        } else {
          setTimeout(function() {
            //要延时执行的代码
            that.getLink();
          }, 500) //延迟时间 这里是1秒
         
          wx.showToast({
            title: "请先拼多多授权",
            icon: 'none'
          })

        }
      },
      fail: function () {
        wx.hideLoading();
      }
    });
  },
  getLink: function (e) {

    // wx.showLoading({
    //   title: '加载中',
    //   mask: false
    // })
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=Pdd&a=getUrl',
      data: {
        token: app.globalData.token,

      },
      header: {
        'Content-type': 'application/x-www-form-urlencoded',
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {

        if (res.data.code == 0) {
          wx.navigateToMiniProgram({
            appId: res.data.data.url_list[0].we_app_info.app_id,
            path: res.data.data.url_list[0].we_app_info.page_path,
            success(res) {
              console.log(res)
            }
          })
        } else {
        
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none'
          })

        }
      },
      fail: function () {
        // wx.hideLoading();
      }
    });
  },
  onShow: function() {
    var that = this;
  },
toShared:function(){
  wx.navigateTo({
    url: '/pages/mine/shared',
  })
},

  createShared:function(){
    wx.navigateTo({
      url: '/pages/shop/create-shared?goodid=' + this.data.goodsList.goods_id,
    })
  },
  //预览图片
  previewImage: function(e) {
    var imgUrl = e.currentTarget.dataset.src;
    wx.previewImage({
      urls: [imgUrl], //需要预览的图片http链接列表，注意是数组
      current: '', // 当前显示图片的http链接，默认是第一个
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  
  //查看图文详情
  toContent: function(e) {
    console.log(e)
    wx.navigateTo({
      url: 'webview?url=' + e.currentTarget.dataset.url,
    })
  },


  //获取商品详情
  getDetail: function(goods_id) {
    
    var that = this;
    var jlV;
    var c1;
    wx.request({
      url: app.globalData.api + '/app.php?c=Pdd&a=getGoodsDetail',
      data: {
        token: app.globalData.token,
        goods_sign: goods_id,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        if (res.data.code == 0) {
          var tempimg;
          jlV = res.data.data.commission_vip / 100
          c1 = res.data.data.goods_details.commission / 100
          for (var i = 0; i < res.data.data.goods_details.goods_gallery_urls.length; i++) {
            tempimg = tempimg + '< img src = ' + res.data.data.goods_details.goods_gallery_urls[i] + ' style = "max-width:100%" />'
          }
          that.setData({
            tempimg: "<html>" + tempimg + "</html>"
          })

          var mingroupPrice = res.data.data.goods_details.min_group_price;
          var coupon_discount = res.data.data.goods_details.coupon_discount;
          var promotion_rate = res.data.data.goods_details.promotion_rate;
          var sum0 = mingroupPrice - coupon_discount;
          var sum1 = sum0 * promotion_rate/1000;
          var sum3 = sum1/100
          // console.log(mingroupPrice)
          // console.log(coupon_discount)
          // console.log(promotion_rate)
          // console.log(sum1)
          // console.log(sum3)

          // 时间戳转化的日期格式： 2019-02-21 10:04:38
          that.setData({
            goodsList: res.data.data.goods_details,
            commission_vip: jlV.toFixed(2),
            group_id:app.globalData.group_id,
            ysqdate: time.formatTimeTwo(res.data.data.goods_details.coupon_end_time, 'Y-M-D h:m:s')
          })
          if(app.globalData.group_id==1){//会员
            that.setData({
              commission: c1.toFixed(2),
            })
          }else{//淘掌柜
            that.setData({
              commission: jlV.toFixed(2),
            })
          }
          console.log(that.data.ysqdate)
          that.getMerchantMsg(res.data.data.goods_details.mall_id);
        } else {
          if (res.data.code == 105) {
            wx.removeStorage({
              key: 'token',
              success: function(res) {
                console.log(res)
              },
            })
            app.globalData.token = '';
            wx.reLaunch({
              url: '/pages/mine/login',
            })
          }
          wx.showToast({
            title: '' + res.data.msg,
          })
        }
      }
    })
  },
  onShareAppMessage: function(e) { 
    if (e.from === 'button') {
      console.log("来自页面内转发按钮");
      console.log(e.target);
      if (e.target.dataset.index==1){
        return {
          title: '快来汇客熊小程序领取优惠券啦',
          desc: '',
          imageUrl: "/static/images/splash.png",
          path: '/pages/live/splash' // 路径，传递参数到指定页面。
        }
      }else{
        return {
          title: '【拼多多】  优惠券' + this.data.goodsList.coupon_discount / 100 + '元券  原价￥' + this.data.goodsList.min_group_price / 100 + '  券后价￥' +
            (this.data.goodsList.min_group_price - this.data.goodsList.coupon_discount) / 100,
          desc: '',
          path: '/pages/live/splash?auth_code=' + app.globalData.auth_code + '&goods_id=' + this.data.goodsList.goods_id // 路径，传递参数到指定页面。
        }
      }
    }else{
      return {
        title: '【拼多多】  优惠券' + this.data.goodsList.coupon_discount / 100 + '元券  原价￥' + this.data.goodsList.min_group_price / 100 + '  券后价￥' +
          (this.data.goodsList.min_group_price - this.data.goodsList.coupon_discount) / 100,
        desc: '',
        path: '/pages/live/splash?auth_code=' + app.globalData.auth_code + '&goods_id=' + this.data.goodsList.goods_id // 路径，传递参数到指定页面。
      }
    }
  },
  
  downloadImg: function () {
    wx.navigateTo({
      url: '/pages/mine/downimg',
    })
  },

  //用户是否收藏商品
  is_collect: function(goods_id) {
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=PddCollect&a=is_collect',
      data: {
        token: app.globalData.token,
        goods_id: goods_id,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        // console.log(res)
        if (res.data.code == 0) {
          if (res.data.data.is_collect == 'Y') {
            that.setData({
              is_collect: true
            })
          } else {
            that.setData({
              is_collect: false
            })
          }
        }
      }
    })
  },

  //收藏商品
  collect: function() {
    var that = this;
    if (app.globalData.token != "") {
      if (that.data.is_collect == true) {
        that.cancel();
      } else {
        that.saveCollect();
      }
    } else {
      wx.reLaunch({
        url: '/pages/mine/login',
      })
    }
  },

  saveCollect: function() {
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=PddCollect&a=collect',
      data: {
        token: app.globalData.token,
        goods_id: that.data.goods_id,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'post',
      dataType: 'json',
      success(res) {
        console.log(res)
        if (res.data.code == 0) {
          that.setData({
            is_collect: true
          })
        } else {
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },

  //取消收藏
  cancel: function() {
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=PddCollect&a=cancelCollect',
      data: {
        token: app.globalData.token,
        goods_id: that.data.goods_id,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        if (res.data.code == 0) {
          that.setData({
            is_collect: false
          })
        } else {
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },

  //分享
  shareds: function() {
    var that = this;
    wx.navigateToMiniProgram({
      appId: 'wx32540bd863b27570',
      path: 'package_a/welfare_coupon/welfare_coupon?goods_id=' + that.data.goods_id + '&pid=8792163_102229125&customParameters=65880&authDuoId=200005&cpsSign=CC_200901_8792163_102229125_02a3fb793ae17af26b7747217ebdb239&duoduo_type=2',
      success(res) {
        console.log(res)
      }
    })
  },

  //领券
  getQuan: function() {
    var that = this;
    wx.navigateToMiniProgram({
      appId: 'wx32540bd863b27570',
      path: 'package_a/welfare_coupon/welfare_coupon?goods_id=' + that.data.goods_id + '&pid=8792163_102229125&customParameters=65880&authDuoId=200005&cpsSign=CC_200901_8792163_102229125_02a3fb793ae17af26b7747217ebdb239&duoduo_type=2',
      success(res) {
        console.log(res)
        // "https://mobile.yangkeduo.com/app.html?use_reload=1&launch_url=duo_coupon_landing.html%3Fgoods_id%3D170398640657%26pid%3D8792163_102229125%26customParameters%3D65880%26cpsSign%3DCC_200901_8792163_102229125_02a3fb793ae17af26b7747217ebdb239%26duoduo_type%3D2&campaign=ddjb&cid=launch_dl_force_"
        // https://mobile.yangkeduo.com/app.html?use_reload=1&launch_url=duo_coupon_landing.html%3Fgoods_id%3D33840894108%26pid%3D8792163_102229125%26customParameters%3D65880%26cpsSign%3DCC_200901_8792163_102229125_52a47191c3da32b3f09cdc3fc08e08dc%26duoduo_type%3D2&campaign=ddjb&cid=launch_dl_force_
      }
    })
  },

  //查询店铺信息
  getMerchantMsg(mall_id) {
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=Pdd&a=getMerchantMsg',
      data: {
        mall_id: mall_id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function(res) {
        // console.log(res);
        that.setData({
          merchantMsg: res.data.data.merchantMsg
        })
      },
      fail: function() {

      }
    });
  },
});